<?php
    //用户注册
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require '../../PHPMailer/Exception.php';
    require '../../PHPMailer/PHPMailer.php';
    require '../../PHPMailer/SMTP.php';
    require '../db.php';

    $username = $_GET["ADM"];
    $password = $_GET["PW"];
    $email = $_GET["EMAIL"];

    $conn = connect("bcoh");

    $result = insert($conn, "users", "'$username','$password','$email'");

    if($result) {
        $email_rand = "$email@" . rand();
        update($conn, "users", "Email='$email_rand'", "UserName='$username'");

        $address = "http://" . $_SERVER['HTTP_HOST'] . "/user/emailverify.html?ADM=$username&EMAIL=" . md5($email_rand);

        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try {
            //服务器配置
            $mail->CharSet ="UTF-8";                     //设定邮件编码
            $mail->SMTPDebug = 0;                        // 调试模式输出
            $mail->isSMTP();                             // 使用SMTP
            $mail->Host = 'smtp.qiye.aliyun.com';                // SMTP服务器
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
            $mail->Username = 'bcoh@snitro.top';                // SMTP 用户名  即邮箱的用户名
            $mail->Password = 'Qdc010325';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
            $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
            $mail->Port = 465;                            // 服务器端口 25 或者465 具体要看邮箱服务器支持
        
            $mail->setFrom('bcoh@snitro.top', 'BCOH邮件系统');  //发件人
            $mail->addAddress($email);  // 收件人
            $mail->addReplyTo('bcoh@snitro.top'); //回复的时候回复给哪个邮箱 建议和发件人一致

            //Content
            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = "感谢注册BCOH";
            $mail->Body    = '<div style="clear: both;"><div style="clear: both;"><br></div><table id="aym_table_1610966972127" style="border-collapse: collapse; border: 1px solid rgb(255, 255, 255);"><tbody><tr><td valign="top" style="border: 1px solid rgb(255, 255, 255); padding: 3px 8px; background-color: rgb(216, 216, 216);" width="1470" class=""><div style="clear: both;"><br></div><h1 style="clear: both;">感谢注册BCOH<br></h1><div style="clear: both;"><br></div><div style="clear: both;"><span style="font-size: 18px;">请您尽快点击下方连接验证邮箱</span></div><div style="clear: both;"><br></div><div style="clear: both;"><span style="font-size: 18px;"><br></span></div><div style="clear: both;"><span style="font-size: 18px;"><a href="' . $address . '" target="_blank">点击此处验证邮箱</a></span></div></td></tr></tbody></table></div>';
            $mail->AltBody = "复制网址至浏览器，以验证邮箱地址    $address";
        
            $mail->send();
        } catch (Exception $e) {
            echo '邮件发送失败: ', $mail->ErrorInfo;
        }

        echo "Success";
    } else {
        $info = mysqli_error($conn);
        if (strpos($info, "Duplicate entry") === 0) {
            $infos = explode("'", $info);

            if ($infos[3] === "users.PRIMARY")
                die("Error 0x01");  //用户名重复
            else if ($infos[3] === "users.Email")
                die("Error 0x02");  //邮箱重复
        }
    }

    close($conn);
?>